﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Launchkit_Backend.model
{
    public class Entry
    {
        public const int TypeCollection = 0;
        public const int TypeEntry = 1;


        public long ID;
        public long ParentID;
        public int Type;
        public string DisplayName;
        public string DisplayIcon;
        public String Data
        {
            get {
                return JsonConvert.SerializeObject(this.EntryData);
            }
            set {
                this.EntryData = JsonConvert.DeserializeObject<EntryData>(value);
            }
        }
        public Entry(long iD) {
            Entry e = DBUtils.getEntryById(iD);
            this.ID = e.ID;
            this.ParentID = e.ParentID;
            this.Type = e.Type;
            this.DisplayName = e.DisplayName;
            this.DisplayIcon = e.DisplayIcon;
            this.Data = e.Data;
        }
        public Entry(long iD, long parentID,int type, string displayName, string displayIcon, string data)
        {
            ID = iD;
            ParentID = parentID;
            Type = type;
            DisplayName = displayName;
            DisplayIcon = displayIcon;
            Data = data;
        }

        public Entry(long parentID, int type, string displayName, string displayIcon, string action,string param)
        {
            ParentID = parentID;
            Type = type;
            DisplayName = displayName;
            DisplayIcon = displayIcon;
            Data = "{}";
            EntryData.action = action;
            EntryData.param = param;
        }

        public List<Entry> ListEntries()
        {
            if(Type == TypeEntry) { return new List<Entry>(); }
            return DBUtils.listEntry(this.ID);
        }

        public bool Save() {
            return DBUtils.addEntry(this);
        }
        public bool Delete() {
            return DBUtils.exec("delete from Entries where id=?", this.ID) > 0;
        }


        public EntryData EntryData{get;set;}

    }
}
